package cn.imvc.core;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.opensymphony.xwork2.ActionContext;

import org.apache.commons.collections.map.ListOrderedMap;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class HttpContext {

	public static ServletContext getContext() {
		return getSession().getServletContext();
	}

	public static HttpServletRequest getRequest() {

		if (ActionContext.getContext() == null) {
			return ((ServletRequestAttributes) RequestContextHolder
					.currentRequestAttributes()).getRequest();
		} else {

			return ServletActionContext.getRequest();
		}

	}

	public static HttpSession getSession() {
		return getRequest().getSession();
	}

	public static Cookie[] getCookie() {
		return getRequest().getCookies();
	}

	public static HttpServletResponse getResponse() {
		return (HttpServletResponse) getRequest().getAttribute(
				"_ServletResponse_");
	}

	public static String getRootUrl() {
		return getContext().getAttribute("rootUrl").toString();
	}

	public static String getRootPath() {
		return getContext().getAttribute("rootPath").toString();
	}

	public static boolean isPost() {
		return getRequest().getMethod().equalsIgnoreCase("POST");
	}

	public static boolean isGet() {
		return getRequest().getMethod().equalsIgnoreCase("GET");
	}

	public static Map<String, Object> getPostJson() {
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(getRequest()
					.getInputStream(), "UTF-8"));
			String line = null;
			StringBuilder sb = new StringBuilder();
			while ((line = br.readLine()) != null) {
				sb.append(line);
			}
			return parseJSON2Map(sb.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;

	}

	@SuppressWarnings("unchecked")
	public static Map<String, Object> parseJSON2Map(String jsonStr) {
		ListOrderedMap map = new ListOrderedMap();
		JSONObject json = JSONObject.fromObject(jsonStr);
		for (Object k : json.keySet()) {
			Object v = json.get(k);
			if (v instanceof JSONArray) {
				List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
				Iterator<JSONObject> it = ((JSONArray) v).iterator();
				while (it.hasNext()) {
					JSONObject json2 = it.next();
					list.add(parseJSON2Map(json2.toString()));
				}
				map.put(k.toString(), list);
			} else {
				map.put(k.toString(), v);
			}
		}
		return map;
	}

}
